<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------
namespace app\Manage\controller;

use Request;
use app\common\controller\Manage;
use app\job\import\User;
use app\job\import\Device as DeviceModel;
use app\job\import\Student as StudentModel;
use app\job\import\StudentType as StudentTypeModel;
use app\job\import\BookPlan as BookPlanModel;
use app\job\import\BookPrice as BookPriceModel;
use app\job\import\BookInfo as BookInfoModel;
use app\job\import\BookAdjust as BookAdjustModel;
use app\job\import\BookRel as BookRelModel;
use app\job\import\Teacher as TeacherModel;
use think\facade\Log;

/**
 * 文件上传
 * Class File
 * @package app\Manage\controller
 */
class Files extends Manage
{
    public function uploadVideo(){
        $result       = [
            'status' => false,
            'msg'    => '上传失败',
            'data'   => []
        ];
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize = config('zlserver.file_size');
        $savepath = '/static/uploads/video';

        $filePath = ROOT_PATH.'public'.$savepath;
        if(!is_dir($filePath)){
            @mkdir($filePath);
        }
        $info = $file->validate(['size'=>$maxSize,'ext'=>'mp4'])->move($filePath);
        if($info){
            $fileName = $info->getFilename();
            $url           = getRealUrl( $savepath.'/'.$info->getSaveName());
            $iData['id']   = md5(get_hash($fileName));
            $iData['type'] = 'local';
            $iData['file_type'] = 'video';
            $iData['name'] = $fileName;
            $iData['url']  = $url;
            $iData['ctime']  = time();
            $iData['path'] = $filePath .'/'.$info->getSaveName();
            $files = new \app\common\model\Files();
            if($files->save($iData)) {
                $result['data'] = $iData;
                $result['status'] = true;
                $result['msg'] = '上传成功';
                return $result;
            }
        }else{
            // 上传失败获取错误信息
            $result['msg'] = $file->getError();
            return $result;
        }
    }

    // 导入文件提交
    public function uploadCsv(){
        $result       = [
            'status' => false,
            'msg'    => '上传失败',
            'data'   => []
        ];
        
        $type = input('type/s', '');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $fileInfo = $file->getInfo();
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize = config('zlserver.file_size');
        $savepath = '/static/uploads/csv';

        $filePath = ROOT_PATH.'public'.$savepath;
        if(!is_dir($filePath)){
            @mkdir($filePath);
        }
 
        $info = $file->validate(['size'=>$maxSize,'ext'=>'csv'])->move($filePath);
        if($info){
            $fileName = $info->getFilename();
            $url           = getRealUrl( $savepath.'/'.$info->getSaveName());
            $iData['id']   = md5(get_hash($fileName));
            $iData['type'] = 'local';
            $iData['file_type'] = 'csv';
            $iData['name'] = $fileInfo['name'];
            $iData['url']  = $url;
            $iData['ctime']  = time();
            $iData['path'] = $filePath .'/'.$info->getSaveName();
            $files = new \app\common\model\Files();
            if($files->save($iData)) {
                // 用户数据导入
                if($type == 'user') {
                    $user = new User();
                    return $user->check($iData['id'], $iData['path']);
                }
            }
        }else{
            // 上传失败获取错误信息
            $result['msg'] = $file->getError();
            return $result;
        }
    }
 
    // 导入文件提交
    public function uploadXls(){
        $result       = [
            'status' => false,
            'msg'    => '上传失败',
            'data'   => []
        ];
        
        $type = input('type/s', '');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $fileInfo = $file->getInfo();
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize = config('zlserver.file_size');
        $savepath = '/static/uploads/xls';

        $filePath = ROOT_PATH.'public'.$savepath;
        if(!is_dir($filePath)){
            @mkdir($filePath);
        }
 
        $info = $file->validate(['size'=>$maxSize,'ext'=>'xls'])->move($filePath);
        if($info){
            $fileName = $info->getFilename();
            $url           = getRealUrl( $savepath.'/'.$info->getSaveName());
            $iData['id']   = md5(get_hash($fileName));
            $iData['type'] = 'local';
            $iData['file_type'] = 'xls';
            $iData['name'] = $fileInfo['name'];
            $iData['url']  = $url;
            $iData['ctime']  = time();
            $iData['path'] = $filePath .'/'.$info->getSaveName();
            Log::Info($iData);
            $files = new \app\common\model\Files();
            if($files->save($iData)) {
                if($type == 'user') { // 用户数据导入 
                    $user = new User();
                    return $user->check($iData['id'], $iData['path']);
                } else if($type == 'device') { // 设备数据导入 
                    $device = new DeviceModel();
                    return $device->check($iData['id'], $iData['path']);
                } else if($type == 'student') { // 学生信息数据导入 
                    $student = new StudentModel();
                    return $student->check($iData['id'], $iData['path']);
                } else if($type == 'bookplan') { // 教材计划导入 
                    $bookPlan = new BookPlanModel();
                    return $bookPlan->check($iData['id'], $iData['path']);
                } else if($type == 'bookprice') { // 教材单价导入 
                    $bookPrice = new BookPriceModel();
                    return $bookPrice->check($iData['id'], $iData['path']);
                } else if($type == 'bookinfo') { // 教材单价导入 
                    $bookInfo = new BookInfoModel();
                    return $bookInfo->check($iData['id'], $iData['path']);
                } else if($type == 'studenttype') { // 学生属性数据导入 
                    $studentTypeModel = new StudentTypeModel();
                    return $studentTypeModel->check($iData['id'], $iData['path']);
                } else if($type == 'bookadjust') { // 非计划退书导入
                    $bookAdjustModel = new BookAdjustModel();
                    return $bookAdjustModel->check($iData['id'], $iData['path']);
                } else if($type == 'bookrel') { //非计划发放教材关联信息导入
                    $bookRelModel = new BookRelModel();
                    return $bookRelModel->check($iData['id'], $iData['path']);
                } else if($type == 'teacher') { //按班级导入辅导员
                    $teacher = new TeacherModel();
                    return $teacher->check($iData['id'], $iData['path']);
                }
            }
        }else{
            // 上传失败获取错误信息
            $result['msg'] = $file->getError();
            return $result;
        }
    }
}
